import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { MessageService } from 'src/app/core/service/message.service';
import { Billing } from '../model/billing';
import { BillingType, BillingTypes, getBillingTypesByValue } from '../model/type';
import { BillingService } from '../service/billing.service';
import { DateUtil } from 'src/app/core/util/date-util';
import { NzMessageService } from 'ng-zorro-antd';
import { ClipboardService } from 'ngx-clipboard';


@Component({
  selector: 'app-billing-list',
  templateUrl: './billing-list.component.html',
  styleUrls: ['./billing-list.component.scss']
})
export class BillingListComponent implements OnInit {

  url = 'billing';

  billingTypes: BillingType[] = BillingTypes;
  billings: Billing[] = [];
  pageNo: number;
  pages: number;

  validateForm: FormGroup;
  controlArray = [];
  isCollapse = true;

  constructor(
    private _clipboardService: ClipboardService,
    private msg: MessageService,
    private fb: FormBuilder,
    private billingService: BillingService,
    private router: Router,
    private nzMessageService: NzMessageService
  ) { }

  submitForm(from): void {
    console.log(from)
    // tslint:disable-next-line:forin
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    this.loadBillings({ pageNo: 1 });
  }

  resetForm(): void {
    this.validateForm.reset();
    this.validateForm.patchValue({ type: 'All' });
  }

  ngOnInit() {
    this.msg.sendMessage(this.url);

    this.validateForm = this.fb.group({
      type: ['All'],
      duration: [null],
      varId: [null]
    });

    this.loadBillings({ pageNo: 1 });
  }

  pageIndexChange(event: null) {
    this.loadBillings({ pageNo: event });
  }

  show(type: string, varId: string) {
    this.router.navigate(['/billing', varId], {
      queryParams: {
        type: type
      }
    });
  }

  cancel(id: string) {
    this.billingService.cancelWithdrawalCash(id).subscribe(res => {
      if (res.status === 0) {
        this.nzMessageService.info('取消成功');
        this.ngOnInit();
      } else {
        this.billingService.error(res);
      }
    }, error => this.billingService.error(error));
  }
  private copy(id): void {
    this._clipboardService.copyFromContent(id);
    this.nzMessageService.info('复制地址成功');
  };

  private loadBillings(data: {
    pageNo: number, startDate?: string,
    endDate?: string,
    type?: string,
    varId?: string
  }) {
    const value = this.validateForm.value as { type: string | null, varId: string | null, duration: Date[] | null };
    if (value.type && value.type !== 'All') {
      data.type = value.type;
    }
    if (value.duration && value.duration.length > 0) {
      data.startDate = DateUtil.format(value.duration[0], 'yyyy/MM/dd');
      data.endDate = DateUtil.format(value.duration[1], 'yyyy/MM/dd');
    }
    data.varId = value.varId;
    this.billingService.listBillings(data).subscribe(res => {
      if (res.status === 0) {
        this.billings = res.data.list;
        this.pageNo = res.data.pageNo;
        this.pages = res.data.pages;
      } else {
        this.billingService.error(res);
      }
    }, error => this.billingService.error(error));
  }

}
